var WebSocketClient = require('websocket').client;
var client = new WebSocketClient();
client.on('connectFailed', function (error) {
    console.log('Connect Error: ' + error.toString());
});

// client.connect('ws://localhost:3001/', 'echo-protocol');
client.connect('ws://localhost:3001/');

module.exports = function (msg) {
    client.on('connect', function (connection) {
        console.log('WebSocket client connected');
        connection.on('error', function (error) {
            console.log('Connection Error: ' + error.toString());
        });
        connection.on('close', function () {
            console.log('echo-protocol Connection Closed');
        });
        connection.on('message', function (message) {
            if (message.type === 'utf8') {
                console.log("Received: '" + message.utf8Data + "'");
            }
        });

        // function sendNumber() {
        if (connection.connected) {
            console.log(msg);
            connection.sendUTF(msg);
            // setTimeout(sendNumber, 1000);
        }
        // }

        // sendNumber();
    });
};
